package com.linkdood.app.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkdood.app.constants.ErrorCode;
import com.linkdood.app.domain.AccountUploadFile;
import com.linkdood.app.domain.UploadFile;
import com.linkdood.app.dto.UploadFileDTO;
import com.linkdood.app.exception.GeneralException;
import com.linkdood.app.exception.InvalidParamException;
import com.linkdood.app.mapper.UploadFileMapper;
import com.linkdood.app.utils.DateTimeUtil;
import com.linkdood.app.utils.FileDigestUtil;
import com.linkdood.app.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 文件上传下载操作功能
 */
public interface UploadFileService extends IService<UploadFile> {


    List<UploadFileDTO> listFilesByAccountId(Integer page, Integer size, String accountId);

    /**
     * 文件上传
     */
    void saveFile(MultipartFile multipartFile, String currentAccountId);

    Page<UploadFileDTO> pageUploadFiles(String name, Integer page, Integer size, String sort, String currentAccountId);

    String getUUIDFileName(String fileName);

    /**
     * 多文件下载zip地址
     */
    String getDownloadFileUrl(String id, String currentAccountId);

    UploadFile getZipFile(String ids);

    /**
     * 分片上传第一次请求
     */
    String uploadFragment(long fileSize);

    /**
     * 分片上传逻辑
     */
    void saveFileAsPatch(String uploadFileId, HttpServletRequest request, String accountId);

    /**
     * 删除多个文件
     */
    void deleteLocalFiles(String ids);

    /**
     * 定时任务删除无效文件
     */
    void deleteInvalidFile();


    /**
     * 定时任务把超过一定时间的文件置为无效
     */
    void setInvalidFile();


    void getDownloadFile(String id, HttpServletResponse response);

    /**
     * 下载 fileUrl 资源中的图片，并生成缩略图
     */
    String uploadImages(String fileUrl, String filenamePrefix, String end);

    List<UploadFile> listFileByPath(String path);

}

